package com.bw.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    //计算年龄
	public static int getAge (Date src) {
		Date date=new Date();
		int age=date.getYear()-src.getYear();
		if (src.getMonth()<date.getMonth()) {
			return age;
		}else if (src.getMonth()>date.getMonth()){
			return age-1;
		}else {
			if (src.getDate()<=date.getDate()) {
				return age;
			}else{
				return age-1;
			}
		}
    }
	
	//计算月初日期
	public static Date getDateByMonthInit (Date src) {
		Calendar c=Calendar.getInstance();
		c.setTime(src);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();	
    }
	//计算月末
	public static Date getDateByMonthLast(Date src) {
		Calendar c=Calendar.getInstance();
		Date dateByMonthInit = getDateByMonthInit(src);
		c.setTime(dateByMonthInit);
		c.add(Calendar.MONTH, +1);
		c.add(Calendar.MILLISECOND, -1);
		return c.getTime();
	}
	
	//求未来日期离今天还剩的天数
	public static int getDaysByFuture (Date future) {
		Date date=new Date();
		int year=future.getYear()-date.getYear();
		int month=future.getMonth()-date.getMonth();
		int day=future.getDay()-date.getDay();
		int num=year*365+month*30+day;
		return num;		
	}
	
	public static int getDaysByDeparted (Date departed) {
		Date date=new Date();
		int year=date.getYear()-departed.getYear();
		int month=date.getMonth()-departed.getMonth();
		int day=date.getDay()-departed.getDay();
		int num=year*365+month*30+day;
		return num;	
	}
}
